package com.yt.sys.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.yt.sys.models.service.IEmpRatingStatsService;
import com.yt.sys.models.bo.EmpRatingStatsBO;
import com.yt.sys.models.dto.EmpRatingStatsDTO;
import java.math.BigDecimal;

import java.util.List;


/**
 * 员工星级控制器
 *
 * @author yzz
 * @Date 2025-09-17 14:31:15
 */

@RestController
@RequestMapping("/api/empRatingStats")
@Slf4j
@Api(value = "EmpRatingStatsApiController",tags = "员工星级Api接口")
public class EmpRatingStatsApiController {

    @DubboReference
    private IEmpRatingStatsService empRatingStatsService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<EmpRatingStatsDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        EmpRatingStatsDTO dto = empRatingStatsService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "员工星级列表",notes = "员工星级列表")
    @PostMapping("/queryPageList")
    public Result<Page<EmpRatingStatsDTO>> queryPageList(EmpRatingStatsBO bo){
        Wrapper<EmpRatingStatsDTO> wrapper = new EntityWrapper<>();
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<EmpRatingStatsDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = empRatingStatsService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @PostMapping("/listAll")
    @ApiOperation(value = "员工星级列表",notes = "员工星级列表")
    public Result<List<EmpRatingStatsDTO>> listAll(){
        Wrapper<EmpRatingStatsDTO> wrapper = new EntityWrapper<>();
        List<EmpRatingStatsDTO> list = empRatingStatsService.selectList(wrapper);

        return new Result<>(list);
    }

    @PostMapping("/getRatingByUserId")
    @ApiOperation(value = "根据用户ID获取员工平均星级", notes = "根据用户ID获取员工平均星级")
    @ApiImplicitParam(name = "userId", value = "员工ID", required = true, paramType = "query", dataType = "Long")
    public Result<BigDecimal> getRatingByUserId(@RequestParam("userId") Long userId) {
        if (userId == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        Wrapper<EmpRatingStatsDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userId);
        EmpRatingStatsDTO dto = empRatingStatsService.selectOne(wrapper);

        if (dto == null) {
            return new Result<>(new BigDecimal("0"));
        }

        return new Result<>(dto.getTotalRating());



    }
}
